﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Wassrile.Config.ConfigPage
{
    public partial class AccountRecv: Krilib.Control.ConfigPage
    {
        public AccountRecv()
        {
            InitializeComponent();
        }

        private void WassrConfigPage_Load(object sender, EventArgs e)
        {
            userId.Text = Core.Config.account.UserName;
            if (Core.Config.account.CredentialType == Account.CredentialTypes.Token)
            {
                useToken.Checked = true;
                tokenPassword.Text = Core.Config.account.PasswordOrToken;
            }
            else
            {
                useBasic.Checked = true;
                basicPassword.Text = Core.Config.account.PasswordOrToken;
            }
            recvInterval.Value = Core.Config.Receive.recvInterval;
            referenceBackPageNum.Value = Core.Config.Receive.getHistoryNum;
            replyCheckInverval.Value = Core.Config.Receive.replyCheckInterval;
            traceFav.Checked = Core.Config.Receive.traceFav;
        }

        private void TokenBasicSw_CheckedChanged(object sender, EventArgs e)
        {
            requestToken.Enabled = useToken.Checked;
            tokenPassword.Enabled = useToken.Checked;
            basicPassword.Enabled = !useToken.Checked;
        }

        public override void Apply()
        {
            string nPassword = useToken.Checked ? tokenPassword.Text : basicPassword.Text;
            if (userId.Text != Core.Config.account.UserName ||
               (Core.Config.account.CredentialType == Account.CredentialTypes.Token) != useToken.Checked ||
                nPassword != Core.Config.account.PasswordOrToken)
            {
                //Re-Init
                if (MessageBox.Show(@"Wassrのアカウント情報が変更されています。
アカウントを変更した後、起動後に一度だけ行う
アカウント周辺情報取得処理が再度実行されます。

アカウントを変更してもよろしいですか？", "Wassrアカウント情報の変更", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    if (useToken.Checked)
                        Core.Config.account.CredentialType = Account.CredentialTypes.Token;
                    else
                        Core.Config.account.CredentialType = Account.CredentialTypes.BASIC;
                    Core.Config.account.UserName = userId.Text;
                    Core.Config.account.PasswordOrToken = nPassword;
                }
            }
            Core.Config.Receive.recvInterval = (uint)recvInterval.Value;
            Core.Config.Receive.getHistoryNum = (uint)referenceBackPageNum.Value;
            Core.Config.Receive.replyCheckInterval = (uint)replyCheckInverval.Value;
            Core.Config.Receive.traceFav = traceFav.Checked;
        }

        private void requestToken_Click(object sender, EventArgs e)
        {
            using (var tokenAuth = new Dialogs.TokenAuth())
            {
                if (tokenAuth.ShowDialog() == DialogResult.OK)
                {
                    tokenPassword.Text = tokenAuth.Token;
                }
            }
        }
    }
}
